// pages/manage/add-class-product/index.js
const httpclient = require('../../../utils/httpclient.js')
const md5Util = require('../../../utils/md5.js')
const util = require('../../../utils/util.js')
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tableOne: "block",
    tableTwo: "none",
    opacity: 0,
    tableOneSelect: "selected",
    tableTwoSelect: "",
    server: app.globalData.server,
    allClass: [{ id: "", name:"全部分类"}],
    currentClass: 0,
    onSale:[],
    soldOut:[],
    hidden:false
  },
  chooseClass:function(e){
    this.setData({
      currentClass: e.detail.value
    })
    this.loadData();
  },
  del:function(e){
    let eleData = e.currentTarget.dataset;
    this.setData({
      hidden: false
    });
    httpclient.req(
      "/manage/product/del",
      {
        id: eleData.id
      },
      'post',
      res => {
        this.setData({
          hidden: true
        });
        var result = res.data.result;
        if (res.data.code == 0) {
          wx.showToast({
            title: "删除成功",
            icon:"success",
            duration: 2000
          });
          this.loadData();
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2000
          });
        }
      },
      function (result) {
        console.log(result)
      }
    );
  },
  up:function(e){
    let eleData = e.currentTarget.dataset;
    this.setData({
      hidden: false
    });
    httpclient.req(
      "/manage/product/up",
      {
        id: eleData.id
      },
      'post',
      res => {
        this.setData({
          hidden: true
        });
        var result = res.data.result;
        if (res.data.code == 0) {
          wx.showToast({
            title: "上架成功",
            icon: "success",
            duration: 2000
          });
          this.loadData();
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2000
          });
        }
      },
      function (result) {
        console.log(result)
      }
    );
  },
  down:function(e){
    let eleData = e.currentTarget.dataset;
    this.setData({
      hidden: false
    });
    httpclient.req(
      "/manage/product/down",
      {
        id: eleData.id
      },
      'post',
      res => {
        this.setData({
          hidden: true
        });
        var result = res.data.result;
        if (res.data.code == 0) {
          wx.showToast({
            title: "下架成功",
            icon: "success",
            duration: 2000
          });
          this.loadData();
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2000
          });
        }
      },
      function (result) {
        console.log(result)
      }
    );
  },
  loadClass:function(){
    this.setData({
      hidden: false
    });
    httpclient.req(
      "/manage/class/list",
      {
        office_id: app.globalData.office_id
      },
      'post',
      res => {
        this.setData({
          hidden: true
        });
        var result = res.data.result;
        if (res.data.code == 0) {
          let list = result.list;
          let data = this.data.allClass;
          data = data.concat(list);
          this.setData({
            allClass: data
          });
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2000
          });
        }
      },
      function (result) {
        console.log(result)
      }
    );
  },
  loadData:function(pageNumber){
    this.setData({
      hidden:false
    });
    httpclient.req(
      "/manage/product/list",
      {
        product_type_id: this.data.allClass[this.data.currentClass].id,
        office_id: app.globalData.office_id
      },
      'post',
       res=> {
        this.setData({
          hidden: true
        });
        var result = res.data.result;
        if (res.data.code == 0) {
          let data = {
            onSale: [],
            soldOut: []
          }
          let list = result.list;
          list.forEach(item=>{
            item.image = JSON.parse(item.image);
            if(item.state == 1){
              data.onSale.push(item);
            }else{
              data.soldOut.push(item);
            }
          });
          this.setData(data);
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2000
          });
        }
      },
      function (result) {
        console.log(result)
      }
    );
  },
  swiper: function (event) {
    var dataset = event.target.dataset;
    var data = {
      tableOne: "none",
      tableTwo: "none",
      tableOneOpacity: 0,
      tableTwoOpacity: 0,
      tableOneSelect: "",
      tableTwoSelect: ""
    };
    data[dataset.value] = "block";
    data[dataset.value + "Select"] = "selected";
    data[dataset.value + "Opacity"] = "1";
    this.setData(data);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData();
    this.loadClass();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})